package com.imooc.springcloud.config;

import com.imooc.springcloud.filter.MyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpMethod;

import java.time.ZonedDateTime;

/**
 * @author 小木
 * @date 2021/4/1
 * @desc
 */
@Configuration
public class GatewayConfig {

    @Autowired
    private MyFilter myFilter;

    @Autowired
    private AuthFilter authFilter;

    @Bean
    @Order
    public RouteLocator customizedRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/java/**")
                        .and().method(HttpMethod.GET)
                        .and().header("name")
                        .filters(f -> f.stripPrefix(1)
                                .addResponseHeader("java-param", "My name is Gateway-Route")
                                .filters(myFilter)                  // 如果继承的是【全局过滤器】，那么此处不需要再次指定filter.
                                .filters(authFilter)                // 添加 [权限过滤器]
                        )
                        .uri("lb://FEIGN-CONSUMER")
                )
                .route(r -> r.path("/seckill/**")
                        .and().after(ZonedDateTime.now().plusMinutes(1))
                        .filters(f -> f.stripPrefix(1))
                        .uri("lb://FEIGN-CONSUMER")
                )
                .build();
    }


}
